import expression.*;
import exercise.*;
import config.DifficultyConfig;
import java.util.List;

/**
 * 综合测试类 - 使用Java断言进行单元测试
 */
public class ComprehensiveTest {
    private static int totalTests = 0;
    private static int passedTests = 0;
    
    public static void main(String[] args) {
        System.out.println("开始执行综合单元测试");
        System.out.println("==================================");
        
        // 执行所有测试
        runTest("加法表达式测试", ComprehensiveTest::testAddExpression);
        runTest("减法表达式测试", ComprehensiveTest::testSubExpression);
        runTest("习题功能测试", ComprehensiveTest::testExerciseFunction);
        runTest("习题集生成测试", ComprehensiveTest::testExerciseSetGeneration);
        
        // 生成测试报告
        generateTestReport();
    }
    
    private static void runTest(String testName, Runnable test) {
        totalTests++;
        try {
            test.run();
            passedTests++;
            System.out.println("PASS: " + testName);
        } catch (AssertionError e) {
            System.out.println("FAIL: " + testName + " - " + e.getMessage());
        } catch (Exception e) {
            System.out.println("ERROR: " + testName + " - " + e.toString());
        }
    }
    
    public static void testAddExpression() {
        AddExpression add = new AddExpression(15, 7);
        assert add.calculate() == 22 : "15+7应该等于22";
        assert add.getOperator().equals("+") : "运算符应该是+";
    }
    
    public static void testSubExpression() {
        SubExpression sub = new SubExpression(20, 8);
        assert sub.calculate() == 12 : "20-8应该等于12";
        assert sub.getOperator().equals("-") : "运算符应该是-";
        
        // 测试自动交换
        SubExpression subSwap = new SubExpression(5, 10);
        assert subSwap.calculate() == 5 : "5-10应自动交换为10-5=5";
    }
    
    public static void testExerciseFunction() {
        AddExpression expr = new AddExpression(6, 9);
        Exercise exercise = new Exercise(expr);
        
        assert exercise.getQuestion().equals("6 + 9 = ") : "问题格式错误";
        
        exercise.setUserAnswer(15);
        assert exercise.isCorrect() : "正确答案应该返回true";
        
        exercise.setUserAnswer(10);
        assert !exercise.isCorrect() : "错误答案应该返回false";
    }
    
    public static void testExerciseSetGeneration() {
        ExerciseSet easySet = new ExerciseSet("easy");
        ExerciseSet mediumSet = new ExerciseSet("medium");
        ExerciseSet hardSet = new ExerciseSet("hard");
        
        assert easySet.getExercises().size() == 10 : "简单难度应该有10道题";
        assert mediumSet.getExercises().size() == 15 : "中等难度应该有15道题";
        assert hardSet.getExercises().size() == 20 : "困难难度应该有20道题";
    }
    
    private static void generateTestReport() {
        System.out.println("\n测试报告");
        System.out.println("总测试数: " + totalTests);
        System.out.println("通过数: " + passedTests);
        System.out.println("失败数: " + (totalTests - passedTests));
        
        if (passedTests == totalTests) {
            System.out.println("所有测试通过！");
        } else {
            System.out.println("有 " + (totalTests - passedTests) + " 个测试失败");
        }
    }
}